// pages/driver/addDriverPhoto/addDriverPhoto.js
const handler = require("../../../utils/function.js");
var app = getApp(),
  api = require("../../../api.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageType: null,
    fontImage: null,
    backImage: null,
    idCardPhoto: null,
    idCardPhotoTest: false,
    driverPhoto: null,
    driverPhotoTest: false,
    positive: '', //驾照正面照片
    reverse: '', //驾照反面照片
    drivers: "",
  },
  // 图片正面上传
  upFontloadFile: function (e) {
    var type = e.currentTarget.dataset.type;
    const that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        /* that.setData({
             backImage: tempFilePaths
         });*/
        wx.showLoading({})
        wx.uploadFile({
          url: api.driver.uploadcar, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'access_token': wx.getStorageSync("access_token"),
            '_uniacid': app.siteInfo.uniacid,
            '_acid': app.siteInfo.acid,
          },
          success: function (res) {
            wx.hideLoading()
            if (res.statusCode == 200) {
              var jon = JSON.parse(res.data);
              if (jon.code == 1) {
                if (type == 'driverPhoto') {
                  wx.setStorageSync("positive", jon.data)
                } else {
                  wx.setStorageSync("idpositive", jon.data)
                }
                that.setData({
                  positive: jon.data
                })
              }
            } else {
              /* wx.showToast({
                    title: res.msg,
                    icon: "none"
                })*/
            }
          }
        })
      }
    })
  },
  // 图片反面上传
  upBackloadFile: function (e) {
    const that = this;
    var type = e.currentTarget.dataset.type;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        /* that.setData({
             backImage: tempFilePaths
         });*/
        wx.showLoading({})
        wx.uploadFile({
          url: api.driver.uploadcar, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test',
            'access_token': wx.getStorageSync("access_token"),
            '_uniacid': app.siteInfo.uniacid,
            '_acid': app.siteInfo.acid,
          },
          success: function (res) {
            wx.hideLoading()
            if (res.statusCode == 200) {
              var jon = JSON.parse(res.data);
              if (jon.code == 1) {
                if (type == 'driverPhoto') {
                  wx.setStorageSync("reverse", jon.data)
                } else {
                  wx.setStorageSync("idreverse", jon.data)
                }
                that.setData({
                  reverse: jon.data
                })
              }
            } else {
              /* wx.showToast({
                   title: res.msg,
                   icon: "none"
               })*/
            }
          }
        })
      }
    })
  },
  // intoPageHandler 页面跳转
  intoPageHandler: function (e) {
    wx.navigateBack({
      url: e.currentTarget.dataset.skip + '?type=' + this.data.pageType + '&reverse=' + this.data.reverse + '&positive=' + this.data.positive + '&driver=' + this.data.driver,
      delta: 1
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /*        wx.setStorageSync("driverss",options.driver)*/
    if (options.type == 'driverPhoto') {
      if (wx.getStorageSync('driving_license') != '') {
        this.setData({
          positive: wx.getStorageSync('driving_license').driverfPhoto,
          reverse: wx.getStorageSync('driving_license').driverPhoto
        })
      }
    }
    if (options.type == 'idCardPhoto') {
      if (wx.getStorageSync('id_card_pic') != '') {
        this.setData({
          positive: wx.getStorageSync('id_card_pic').id_card_pic,
          reverse: wx.getStorageSync('id_card_pic').id_card_fpic
        })
      }

      console.log('正面正面', this.data.reverse)
      console.log('正面正面22', this.data.pageType)

    }
    this.setData({
      pageType: options.type,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var positive = wx.getStorageSync("positive") || '';
    var reverse = wx.getStorageSync("reverse") || '';
    this.setData({
      driver: wx.getStorageSync("driver_information"), positive, reverse
    });
  },
})